package org.lansir.af.ui;

import java.util.Calendar;
import java.util.List;

import org.lansir.af.R;
import org.lansir.af.dataview.DataViewActivity;
import org.lansir.af.dataview.DataViewItem;
import org.lansir.af.dataview.DataViewItemActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class CalendarDayFragment extends CommonFragment {

	private int year, month, day;
	private DataViewActivity dataViewActivity;
	private List<DataViewItem> mListDataViewItems;
	private ListView lvCalendarDayActivities;
	private LayoutInflater mInflater;
	private CalendarDayAdapter mCalendarDayAdapter = null;
	private Calendar cal;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = this.getActivity().getIntent();
		year = intent.getIntExtra("Year", -1);
		month = intent.getIntExtra("Month", -1);
		day = intent.getIntExtra("Day", -1);
		cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.DAY_OF_MONTH, day);
		dataViewActivity = new DataViewActivity(this.userdb);
		
			
		refreshData();
		mCalendarDayAdapter = new CalendarDayAdapter();
		
	}
	
	private void refreshData(){
		dataViewActivity.reloadTable();
		dataViewActivity.filterData(cal, CommonFragment.ViewType.DAY);
		mListDataViewItems = dataViewActivity.getRowsForView();
		if(mListDataViewItems.size() == 0){
			getActivity().setResult(Activity.RESULT_OK);
			getActivity().finish();
		}
		if(mCalendarDayAdapter != null)
			mCalendarDayAdapter.notifyDataSetChanged();
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View viewRoot = null;
		mInflater = inflater;
		viewRoot = inflater.inflate(R.layout.fragment_calendar_cell, null);
		lvCalendarDayActivities = (ListView) viewRoot.findViewById(R.id.lvCalendarCellActivities);
		lvCalendarDayActivities.setAdapter(mCalendarDayAdapter);
		lvCalendarDayActivities.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				bundleOtherDataStartup.putLong(CommonFragment.requestBundleRowId, mListDataViewItems.get(position).getId());
				openActivityForResult(CommonFragment.REQUEST_CODE_DEFAULT, "org.lansir.af.intent.action.ACTION_MODE_VIEW_ACTIVITYINFO");
			}
			
		});
		// lvCalendarDayActivities.setAdapter(new ArrayAdapter());
		return viewRoot;
	}
	
	private class CalendarDayAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return mListDataViewItems.size();
		}

		@Override
		public Object getItem(int position) {
			return mListDataViewItems.get(position);
		}

		@Override
		public long getItemId(int position) {
			return mListDataViewItems.get(position).hashCode();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
			if(convertView == null){
				convertView = mInflater.inflate(R.layout.fragment_schedule_listitem_content, null);
				convertView.setClickable(false);
				convertView.setFocusable(false);
				viewHolder = new ViewHolder();
				viewHolder.tvActivityContentTitle = (TextView) convertView.findViewById(R.id.tvActivityContentTitle);
				viewHolder.tvActivityContentTime = (TextView) convertView.findViewById(R.id.tvActivityContentTime);
				viewHolder.tvActivityContentOrganizer = (TextView) convertView.findViewById(R.id.tvActivityContentOrganizer);
				viewHolder.ivActivityContentRepeat = (ImageView) convertView.findViewById(R.id.ivActivityContentRepeat);
				viewHolder.ivActivityContentAlarm = (ImageView) convertView.findViewById(R.id.ivActivityContentAlarm);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder) convertView.getTag();
			}
			DataViewItemActivity dataViewItemActivity = (DataViewItemActivity)mListDataViewItems.get(position);
			viewHolder.tvActivityContentTitle.setText(dataViewItemActivity.getSubject());
			viewHolder.tvActivityContentTime.setText(utils.getTime(dataViewItemActivity.getStartDate()));
			viewHolder.tvActivityContentOrganizer.setText(String.format(utils.getResStr(R.string.strAgendaActivityItemOrganizer), dataViewItemActivity.getOrganizer()));
			viewHolder.ivActivityContentRepeat.setVisibility(dataViewItemActivity.isRepeat() ? View.VISIBLE : View.GONE);
			viewHolder.ivActivityContentAlarm.setVisibility(dataViewItemActivity.isAlarm() ? View.VISIBLE : View.GONE);
			return convertView;
		}
		
		private class ViewHolder{
			public TextView tvActivityContentTitle;
			public ImageView ivActivityContentRepeat;
			public ImageView ivActivityContentAlarm;
			public TextView tvActivityContentTime;
			public TextView tvActivityContentOrganizer ;
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode == Activity.RESULT_OK){
			switch(requestCode){
			case REQUEST_CODE_DEFAULT:
				refreshData();
				
				break;
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	

}
